﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KTXD.Entities;
using KTXD.Data;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace UserKTXD
{
    public partial class Chi_Tiet_Loai_Chuyen_Muc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["Id"] != null)
                {
                    hienThiLoaiChuyenMuc(Convert.ToInt32(Request.Params["Id"]));
                    pagination.CurrentIndex = 1;
                    HienthiDSLoaiChuyenMuc(pagination.CurrentIndex, Convert.ToInt32(Request.Params["Id"]));
                }
            }
        }

        private void HienthiDSLoaiChuyenMuc(int PageNo, int idLoaiChuyenMuc)
        {
            //Hiển thị DS Loại Chuyên mục
            int TotalRows = 0;
            DataSet dsChuyenMuc;
            dsChuyenMuc = DataRepository.Provider.ExecuteDataSet("ChuyenMuc_GetPaged", "1=1 and TrangThai=1 and IdLoaiChuyenMuc=" + idLoaiChuyenMuc, "NgayCapNhat DESC", PageNo - 1, pagination.PageSize);
            rptLoaiChuyenMuc.DataSource = dsChuyenMuc.Tables[0];
            rptLoaiChuyenMuc.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsChuyenMuc.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            pagination.ItemCount = TotalRows;
            if (TotalRows <= pagination.PageSize)
                pagination.Visible = false;
            else
                pagination.Visible = true;
        }

        protected void Pager1_Command(object sender, CommandEventArgs e)
        {
            try
            {
                int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
                pagination.CurrentIndex = currnetPageIndex;
                HienthiDSLoaiChuyenMuc(pagination.CurrentIndex,Convert.ToInt32(Request.Params["Id"]));
            }
            catch (Exception ex)
            {
                Common.WriteLog("Chi-Tiet-Loai-Dich-Vu.aspx.cs", "Pager1_Command()", ex.ToString());
            }
        }

        private void hienThiLoaiChuyenMuc(int id)
        {
            LoaiChuyenMuc ent_LoaiChuyenMuc = DataRepository.LoaiChuyenMucProvider.GetById(id);
            lblSubChuyenMuc.Text = ent_LoaiChuyenMuc.TenLoaiChuyenMuc;
            lblTieuDe.Text = ent_LoaiChuyenMuc.TenLoaiChuyenMuc;
            TList<ChuyenMuc> TList_ChuyenMuc = DataRepository.ChuyenMucProvider.GetByIdLoaiChuyenMuc(id);
            if (TList_ChuyenMuc.Count > 0)
            {
                int _view = 0;
                _view += TList_ChuyenMuc[0].LuotXem;
                _view += 1;
                string query = string.Empty;
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
                conn.Open();
                query = "UPDATE ChuyenMuc SET LuotXem = '" + _view + "' where Id = '" + TList_ChuyenMuc[0].Id + "'";
                SqlCommand sqlCommand = new SqlCommand();
                sqlCommand.Connection = conn;
                sqlCommand.CommandText = query;
                sqlCommand.ExecuteNonQuery();
                conn.Close();
            }
        }

        public string GetTieuDe(object Char)
        {
            string s = Char.ToString();
            return Common.ConvertToUnSign(s);
        }
    }
}